package com.qyer.commons.mc.reloader;

import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * User: Z J Wu Date: 2015/08/12 Time: 22:10 Package: com.qyer.dsp.cache
 */
public abstract class Reloader<T> {

  protected String reloadResource;

  protected AtomicInteger successCnt = new AtomicInteger(0);

  protected AtomicInteger failureCnt = new AtomicInteger(0);

  public Reloader(String reloadResource) {
    if (StringUtils.isBlank(reloadResource)) {
      throw new RuntimeException("Reload resources is blank");
    }
    this.reloadResource = reloadResource;
  }

  public abstract T _reload() throws Exception;

  public T reload() throws Exception {
    try {
      T t = _reload();
      this.successCnt.incrementAndGet();
      return t;
    } catch (Exception e) {
      this.failureCnt.incrementAndGet();
      throw e;
    }
  }

  public String getReloadResource() {
    return reloadResource;
  }

  public int getSuccessCnt() {
    return successCnt.get();
  }

  public int getFailureCnt() {
    return failureCnt.get();
  }
}
